define(function(require, exports){
	var $ = require('jquery');
	var util = require('util');
	var pubjs	= require('pubjs');
	var popwin	= require('@base/dialog').base;
	var view = require('@base/view');
	var grid = require('@base/grid');

	// 活动编辑 —— 选择创意包内容视图
	var SelectCustomer = popwin.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('选择联系人'),
				'width': 900,
				'height': 600,
				'showSub': true,
				'class': 'P-messageManageEditPopwin',
				'noFilter': false,
				// 配置getData获取的数据是否为ids
				'selectIds': false,
				'view_model': {}
			});
			
			this.$data = {};
			this.$selected = [];
			this.$ids = [];
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			self.Super('afterBuild',arguments);
			var el = self.getContainer('body');
			el.addClass('uk-form');
			var c = this.getConfig();
			el.append($([
				'<div class="M-tagLabelsWrapper">',
				'<div pub-mod="@base/taglabels.listType" pub-name="type"></div>',
				'<div pub-mod="@base/taglabels.listType" pub-name="categoryId"></div>',
				'</div>',
				'<div pub-name="grid" pub-mod="/project/modules/selectUser.customerGrid"></div>'
			].join('')));
			
			if(c.noFilter)
			{
				el.find('.M-tagLabelsWrapper').hide();
			}
			
			var config = {
				'type':{
					'all':true,
					'collapse': true,
					'all_label':LANG('所有'),
					'title': LANG('类型：'),
					'data': [null, LANG('代理'), LANG('直客')]
				},
				'categoryId':{
					'all':true,
					'collapse': true,
					'all_label':LANG('所有'),
					'title': LANG('版本：'),
					'data': [null, LANG('有产品版'), LANG('无产品版')]
				},
				'grid': util.extend(this.getConfig('grid') || {}, {"param": {"Depth": 1}})
			};
			
			self.buildFromTemplate(config);
		},
		afterBuildTemplate: function(mod, data){
			var user = pubjs.getUser();
			
			if(this.getConfig('showSub'))
			{
				var grid = this.$.grid;
				var filter = grid.getContainer("filter");
				
				var showSub = $('<label><input type="checkbox"/>'+ LANG('显示从属子帐号') +'</label>');
				showSub.appendTo(filter);
				this.uiBind( showSub, 'click', 'eventShowSubClick' );
				
				if(user.current.level > 1){
					mod[0].hide();	// 隐藏过滤帐号级别功能
					filter.hide();	// 隐藏附加从属子帐号功能
				}
			}
		},
		setParam: function (param) {
			this.$.grid.setParam(param);
			return this;
		},
		reload: function()
		{
			this.$.grid.reload();
		},
		onListTypeChange: function(ev){
			var grid = this.$.grid;
			switch(ev.name){
				case 'type':// 类型：1代理 2直客
					this.$Type = ev.param.type;
					break;
				case 'categoryId':// 版本：1有产品版，2无产品版
					this.$CategoryId = ev.param.type;
					break;
			}
			grid.setParam({
				'Type':this.$Type,
				'CategoryId':this.$CategoryId
			});
			grid.load();
			return false;
		},
		// 显示从属子帐号
		eventShowSubClick: function(evt, elm){
			var grid = this.$.grid;
			var isChecked = $(elm).find('input').prop('checked');
			
			var Depth = isChecked ? 0 : 1;
			grid.setParam({
				'Depth': Depth
			});
			grid.load();
		},
		onOk: function(){
			this.hide();
			this.fire('selectCustomer', this.getData());
			return false;
		},
		onChangeSelect: function(ev) {
			var data = this.$data;
			util.each(ev.param.data, function(item, idx){
				data[item.UserId] = {
					'_id': item._id,
					'UserId': item.UserId,
					'Name': item.Name
				};
			});
			return false;
		},
		setData: function(ids, data) {
			this.$.grid.setSelectRowIds(ids);
			if(data)
			{
				this.$data = data;
			}
			
			return this;
		},
		getData: function(){
			var result = [];
			var ids = this.$.grid.getSelectRowIds();
			var c = this.getConfig();
			if (c.selectIds)
			{
				return ids;
			}
			else
			{
				var data = this.$data;
				util.each(ids, function(id, idx){
					if(data[id]){
						result.push(data[id]);
					}
				});
				return result;
			}
		},
		reset: function(){
			this.$data = {};
			this.$selected = [];
			this.$ids = [];
			// 设置好grid
			var grid = this.$.grid;
			grid.setSelectRowIds([]);
			// 重新加载数据
			grid.reload();
			return this;
		}
	});
	exports.selectCustomer = SelectCustomer;
	
	var CustomerGrid = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
					'reqType': 'websocket',
					'url': 'user/list',
					'cols': [
						{name:"UserId",text: 'ID', type:'fixed',sort:false,align:'center', width:60},
						{name: "Name", text: LANG("用户名称"), type: "index", align: "left", width: 400, render: 'renderName'},
						//{name: "Name", text: LANG("邮箱"), align: "left", width: 250, sort: false},
						{name: "Type", text: LANG("类型"), align: "center", render: 'renderType', sort: false, width: 100},
						{name: "ExchangeInfo", text: LANG("版本"), align: "center", render: 'renderCategory', sort: false, width: 100}
					],
					'sub_field': 'UserId',	// 索引字段名称
					'hasAmount': false,		// 是否有总计模块
					'hasPager': true,		// 是否有分页模块
					'hasExport': false,		// 是否有导出模块
					'hasBatch': false,		// 是否有批量操作
					'hasTab': false,			// 是否显示指标分组模块
					'hasRefresh': false,		// 刷新控件
					'hasSearch': true,
					//'singleSelect': true,
					'pager':{
						"size":10,
						"bounds":5,
						"firstLast":false,
						"showJumper":0,
						"showSizeTypes":0
					},
					'hasSelect': true,
					'layout': {
						"rows":['search(.pb10)(auto)|filter(.h30 lh30 ml20)(auto)']
					},
					'list':{
						'rowSelect': true,
						'scroll_type': 'row',
						'scroll_size': 5
					},
					'sort': 'CreateTime'
				}
			);
			this.Super('init', arguments);
		},
		renderName: function(i, val, row, con){
			var html = $([
				'<div class="uk-text-truncate tl fl" title="'+(val||'')+'">',
				(val||''),
				'</div>'
			].join('')).width(con.width-20);
			return html;
		},
		// 根据type的数字生成对应类型名称
		renderType: function (i, val, row, con) {
			var result = '';
			switch (val) {
				case 2:
					result = LANG('直客');
					break;
				case 3:
					result = LANG('员工');
					break;
				case 4:
					result = LANG('管理员');
					break;
				default: // 1
					result = LANG('代理');
					break;
			}
			return result
		},
		// 根据CategoryId生成对应种类名称
		renderCategory: function (i, val, row, con) {
			var result = '';
			
			switch (val && val.CategoryId) {
				case 2:
					result = LANG('无产品版')
					break;
				default: // 1
					result = LANG('有产品版')
					break;
			}
			return result
		}
	});
	
	exports.customerGrid = CustomerGrid;
});
